/*  
 *  Copyright 2006 Paul Jack.
 *
 *  This file is part of the Dex compiler suite. 
 *  
 *  Dex is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or (at your
 *  option) any later version.
 *  
 *  Dex is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 *  Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */
package dex.compiler.model.statement;


import java.util.Collection;
import java.util.List;

import dex.compiler.model.base.Place;
import dex.misc.Conversion;
import dex.misc.Converter;


/**
 * A block of other statements.  For instance:
 * 
 * <pre>
 * {
 *     statementOne()
 *     statementTwo()
 * }
 * </pre>
 */
public class Block extends Statement {
	

	/**
	 * The list of statements in the block.
	 */
	private List<Statement> statements;

	
	/**
	 * Constructs a new block of statements.
	 * 
	 * @param place  the place where the block originated
	 * @param statements  the statements in the block
	 */
	public Block(Place place, List<Statement> statements) {
		super(place);
		this.statements = statements;
	}

	@Override
	public String toString() {
		StringBuilder result = new StringBuilder("{\n");
		for (Statement s : statements) {
			result.append('\t').append(s).append("\n");
		}
		result.append("}\n");
		return result.toString();
	}


	/**
	 * Returns the statements in this block.
	 * 
	 * @return Returns the statements.
	 */
	public List<Statement> getStatements() {
		return statements;
	}


	/**
	 * Returns the statements in this block, minus any comments.
	 * 
	 * @return  the statements in this block
	 */
	public Collection<Statement> getActions() {
		return new Conversion<Statement,Statement>(statements, new Converter<Statement,Statement>() {
			public Statement convert(Statement s) {
				if (s instanceof Comment) {
					return null;
				}
				return s;
			}
		});
	}


	@Override
	public boolean terminates() {
		for (Statement s: getActions()) {
			if (s.terminates()) {
				return true;
			}
		}
		return false;
	}

}
